cc.Class({
    extends: cc.Component,

    properties: {
        btnStartGame:cc.Node,
        btnAuto:cc.Node,
        btnBet:cc.Node,
        imgCombo:cc.Node,
        btnPrizeRecord:cc.Node,
        listBet:cc.Node,
        itemBet:cc.Prefab,
        listRecord:cc.Node,
        itemRecord:cc.Prefab,
        autoSps:{
            default:[],
            type:cc.SpriteFrame
        }
    },
    setController: function(controller){
        this.controller = controller;
    },
    onLoad ()
    {
        this.btnStartGame.on('click',this.onStartGameBtn,this);
        this.btnAuto.on('click',this.onAutoBtn,this);
        this.autoAnimSp = this.btnAuto.getChildByName("anim_auto");
        this.btnBet.on('click',this.onBetBtn,this);
        this.btnPrizeRecord.on('click',this.onPrizeRecordBtn,this);
        this.comboCount = this.imgCombo.getChildByName('count_combo');
        this.curBetCount = this.btnBet.getChildByName('count');

        this.imgCombo.active = false;
        this.betListShow = false;
        this._autoRun = false;
        this.autoAnimSp.active = false;
        this.showBetList();
    },
    start(){
        this.initBetInfo();
    },
    initBetInfo()
    {
        this.curBetCount.getComponent(cc.Label).string = this.controller.getCurBetCount().toString();
        //读取json

        /**************/
        var itemList = []
        for(var i = 0;i < 6;i++)
        {
            var item = cc.instantiate(this.itemBet);
            item.parent = this.listBet;
            itemList.push(item);
        }
        var betConfig = ["100","500","1000","5000","10000","50000"];

        for(var i = 0;i < itemList.length;i++)
        {
            var lb = itemList[i].getChildByName("betCount");
            itemList[i].name = (betConfig[i]).toString();
            //var button = itemList[i].getComponent(cc.Button);
            itemList[i].on('click', this.onChooseBet,this);
            lb.getComponent(cc.Label).string = betConfig[i].toString();
        }
    },
    onStartGameBtn:function()
    {
        this.controller.startGame();
        this.recoverStartBtn(false);
    },
    onAutoBtn:function()
    {
        this._autoRun = !this._autoRun;
        this.showAutoRun();
        this.recoverStartBtn(false);
        this.controller.autoPlay(this._autoRun);
    },
    onChooseBet:function(event)
    {
        var node = event.target;
        this.curBetCount.getComponent(cc.Label).string = node.name;
        this.controller.setBetCount(parseInt(node.name));
        this.onBetBtn();
    },
    onBetBtn:function(){
        this.betListShow = !this.betListShow
        this.showBetList();
    },
    onPrizeRecordBtn:function(){

    },
    recoverStartBtn:function(enable = false)
    {
        this.btnStartGame.getComponent(cc.Button).interactable = enable;
        this.btnBet.getComponent(cc.Button).interactable = enable;
    },
    refreshComboCount:function(count)
    {
        this.imgCombo.active = true;
        this.comboCount.getComponent(cc.Label).string = count;
    },
    showAutoRun:function()
    {
        var sp = this.btnAuto.getChildByName("txt_auto")
        if(this._autoRun)
        {
            sp.getComponent(cc.Sprite).spriteFrame = this.autoSps[1];
            this.autoAnimSp.active = true;
            var anim = this.autoAnimSp.getComponent(cc.Animation);
            anim.play();
        }else{
            this.autoAnimSp.active = false;
            sp.getComponent(cc.Sprite).spriteFrame = this.autoSps[0];
        }
    },
    getCurBet()
    {
        var ct = this.curBetCount.getComponent(cc.Label).string;
        return parseInt(ct);
    },
    showBetList:function()
    {
        this.listBet.active = this.betListShow;;
    },
});
